package com.ibl.rover.lite.client.Activity;

import java.util.Calendar;
import java.util.List;

import com.ibl.rover.lite.client.Logger;
import com.ibl.rover.lite.client.R;
import com.ibl.rover.lite.client.Application.ApplicationContext;
import com.ibl.rover.lite.common.bean.OrderBean;
import com.ibl.rover.lite.common.bean.ProductBean;
import com.ibl.rover.lite.common.bean.ProductCategroy;
import com.ibl.rover.lite.common.db.Criteria;
import com.ibl.rover.lite.common.db.Criteria.Operators;
import com.ibl.rover.lite.common.db.DatabaseManager;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.TableRow.LayoutParams;

public class ProductReportActivity extends Activity implements OnClickListener {

	private Logger logger = new Logger();
	private ApplicationContext roverApp;
	private DatabaseManager manager = null;
	private Spinner productSpinner = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		try {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.product_report);
			initialize();
		} catch (Exception e) {
			logger.logException("onCreate Exception ", e);
		}
	}

	private void initialize() {
		try {

			roverApp = (ApplicationContext) getApplication();
			manager = roverApp.getDatabaseManager();

			productSpinner = (Spinner) findViewById(R.id.product_spinner);

			ArrayAdapter<CharSequence> productCategoryAdapter = new ArrayAdapter<CharSequence>(
					this, android.R.layout.simple_spinner_item,
					getProductCategory());
			productCategoryAdapter
					.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			productSpinner.setAdapter(productCategoryAdapter);

			productSpinner.setSelection(0);
			getProducts();

			((Button) findViewById(R.id.search_report_product))
					.setOnClickListener(this);
		} catch (Exception e) {
			logger.logException("initialize Exception", e);

		}

	}

	private String[] getProductCategory() {
		try {
			List<ProductCategroy> list = manager
					.findAllBean(ProductCategroy.class);
			String[] productCategoryList = new String[list.size()];
			int i = 0;
			for (ProductCategroy productBean : list) {
				productCategoryList[i++] = productBean.getCategoryCode();
			}
			return productCategoryList;
		} catch (Exception e) {
			logger.logException("get Prodcut Exception", e);

		}

		return null;
	}

	private void getProducts() {
		try {
			String productCategory = productSpinner.getSelectedItem()
					.toString();

			Criteria<ProductBean> criteria = manager
					.generateCriteria(ProductBean.class);
			criteria.addRestriction(ProductBean.COL_PRODUCT_CATEGORY_ID,
					Operators.isEqual, productCategory);
			buildOrderReport(criteria.execute());

		} catch (Exception e) {
			logger.logException("getProducts", e);
		}

	}

	private void buildOrderReport(List<ProductBean> list) {
		try {
			TableLayout productTable = (TableLayout) findViewById(R.id.report_order_table);
			int textColor = getResources().getColor(
					R.color.table_item_text_color);
			productTable.setVisibility(TableLayout.VISIBLE);
			if (productTable.getChildCount() > 1) {
				productTable.removeViews(1, productTable.getChildCount() - 1);
			}
			LayoutParams otherParams = new LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

			otherParams.setMargins(1, 0, 0, 1);

			for (ProductBean productBean : list) {
				TableRow row = new TableRow(this);
				row.setTag(productBean);

				TextView txtOrderId = new TextView(this);
				txtOrderId.setTextAppearance(this, R.style.table_item_font);
				txtOrderId.setText(productBean.getDescription());
				txtOrderId.setGravity(Gravity.CENTER_HORIZONTAL
						| Gravity.CENTER_VERTICAL);
				txtOrderId.setPadding(5, 0, 5, 0);
				txtOrderId.setTextColor(textColor);

				row.addView(txtOrderId, otherParams);

				TextView txtOrderAmount = new TextView(this);
				txtOrderAmount.setTextAppearance(this, R.style.table_item_font);
				txtOrderAmount.setText(productBean.getUnitPrice());
				txtOrderAmount.setGravity(Gravity.CENTER_HORIZONTAL
						| Gravity.CENTER_VERTICAL);
				txtOrderAmount.setPadding(5, 0, 5, 0);
				txtOrderAmount.setTextColor(textColor);

				row.addView(txtOrderAmount, otherParams);

				productTable.addView(row);
				productTable.invalidate();
			}
		} catch (Exception e) {
			logger.logException("build Order Report Excepttion ", e);
		}

	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.search_report_product:
			getProducts();
			break;

		default:
			break;
		}

	}

}
